<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="saveDialog" type="primary">确 定</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import { driveCancel, } from '@/api/potenCus/customer/driveManagement';
import { searchOnceDriveMileage } from '@/api/potenCus/basedata/onceDriveMileage';
import { dictionary } from '@/utils/dictMixin';


export default {
  name: 'EntryMileage',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'mileage'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      onceDriveMileage:''
    };
  },
  watch: {
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      if (this.type === 'mileage' ) {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
      let res = await searchOnceDriveMileage(this.saveData);
      // console.log('单次试驾里程',res)
      var self = res.data
      self.map( (item,index) => {
        if( item.paramCode == "test_drive_mileage" ){
          this.paramValue = Number(item.paramValue)
        }
      })
      this.onceDriveMileage = this.paramValue
      console.log('单次试驾里程',this.onceDriveMileage)
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '开始里程',
          fieldName: 'driveSKm',
          placeholder: '开始里程',
          style: { width: '200px' },
          disabled: true,
          unitRender: () => {
            return <span>KM</span>
          },
        },
        {
          type: 'INPUT',
          label: '结束里程',
          fieldName: 'driveEKm',
          placeholder: '请输入结束里程',
          style: { width: '200px' },
          onInput: val=>{
            let value = val.replace(/^(\-)*(\d+)\.(\d).*$/, '$1$2.$3')
            if(value -this.formList[0].initialValue >= 0){
              this.formList[2].initialValue = Math.floor(value*10 -this.formList[0].initialValue*10) / 10
            }else{
              // console.log(0,"val -this.formList[0].initialValue")
            }
          },
          rules: [{ required: true, message: '请输入结束里程', trigger: 'blur' }],
          unitRender: () => {
            return <span>KM</span>
          },
          change: val => {
            this.changeCancel(val);
          }
        },
        {
          type: 'INPUT',
          label: '本次行驶',
          fieldName: 'driveRunKm',
          placeholder: '本次行驶',
          initialValue:"",
          style: { width: '200px' },
          unitRender: () => {
            return <span>KM</span>
          },
          disabled: true
        },
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    changeCancel(val){
      // console.log('结束里程', val);
    },
    async saveDialog() {
      // console.log('保存的单次里程',this.onceDriveMileage)
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'mileage') {
          console.log(22222, this.saveData.driveEKm*10,this.rowData.driveSKm*10 )
          if(Number(this.saveData.driveEKm)<=Number(this.rowData.driveSKm) ){
            this.$message.info("结束里程必须大于开始里程!");
          }
          else if(this.onceDriveMileage){
            if((Number(this.saveData.driveEKm)*10-Number(this.rowData.driveSKm)*10)/10 >this.onceDriveMileage) {
              this.$message.warning("本次行驶里程已超出" + this.onceDriveMileage + 'KM');
              res = await driveCancel({ ...this.saveData, itemId: this.rowData.itemId, driveStatus: 20211003 });
            } else {
              res = await driveCancel({ ...this.saveData, itemId: this.rowData.itemId, driveStatus: 20211003 });
            }
          }else{
            res = await driveCancel({ ...this.saveData, itemId: this.rowData.itemId, driveStatus: 20211003 });
          }
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },

    closeDialog(param) {
      this.$emit('close', false, param);
    },
  }
};
</script>

<style lang="less" scoped>
</style>

